/*	$Id: test.c 983 2009-09-01 02:45:42Z phrakt $	*/
/*
 * Copyright (c) 2008 Jean-Francois Brousseau <phrakt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/time.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <canopy/log.h>

#include <http/http.h>

#include "test.h"


extern cnp_log_t *cnp_logchan;


static unsigned int    test_count_total;
static unsigned int    test_count_ok;
static unsigned int    test_count_failed;

static char            test_name[128];
static struct timeval  test_start_time;
static struct timeval  test_end_time;


static FILE *test_log_file;
static FILE *test_res_file;

static void test_loghdlr   (struct cnp_log_msg *, void *);


void
test_init(void)
{
	cnp_logmask_t mask;

	CNP_LOG_CLRMASK(mask);

	cnp_log_setmask(cnp_logchan, mask);
	cnp_log_addhdlr(cnp_logchan, mask, test_loghdlr, NULL);

#ifdef TEST_HTTP
	cnp_log_setmask(http_logchan, mask);
	cnp_log_addhdlr(http_logchan, mask, test_loghdlr, NULL);
#endif TEST_HTTP

	if ((test_log_file = fopen("test.log", "w")) == NULL) {
		warn("failed to open test.log for writing");
	}

	if ((test_res_file = fopen("/tmp/test.res", "a")) == NULL) {
		warn("failed to open /tmp/test.res for appending");
	}

	test_log("Starting test run");

	test_count_total = 0;
	test_count_ok = 0;
}

void
test_cleanup(void)
{
	float rate;

	if (test_count_total > 0) {
		rate = (float)test_count_ok / (float)test_count_total * 100;

		printf("\nTest results: %u tests executed, %u successful "
		    "(%2.2f%% success rate)\n\n", test_count_total,
		    test_count_ok, rate);
	}
	else {
		printf("\nTest results: No tests executed\n\n");
	}

	fclose(test_log_file);
	fclose(test_res_file);

}

void
test_start(const char *desc)
{
	size_t len;
	char dots[41] = "........................................";

	len = strlcpy(test_name, desc, sizeof(test_name));
	if (len >= sizeof(test_name))
		len = sizeof(test_name) - 1;

	dots[40 - len] = '\0';

	test_count_total++;

	printf("Running test %s %s ", test_name, dots);
	test_log("\nRunning test %s\n", test_name, dots);

	(void)gettimeofday(&test_start_time, NULL);
}

void
test_end(int result)
{
	struct timeval duration;

	(void)gettimeofday(&test_end_time, NULL);

	timersub(&test_end_time, &test_start_time, &duration);

	if (result == TEST_OK) {
		printf("OK\n");
		test_count_ok++;
	}
	else {
		printf("FAILED\n");
		test_count_failed++;
	}

	fprintf(test_res_file, "%s %u.%lu %s\n", test_name,
	    duration.tv_sec, duration.tv_usec,
	    (result == TEST_OK) ? "OK" : "FAILED");
}


void
test_log(const char *msg, ...)
{
	char buf[1024];
	va_list vap;

	va_start(vap, msg);
	vsnprintf(buf, sizeof(buf), msg, vap);
	va_end(vap);

	fprintf(test_log_file, "%s\n", buf);
}


static void
test_loghdlr(struct cnp_log_msg *msg, void *arg)
{
	fprintf(test_log_file, "%s\n", msg->msg_string);
}
